extensions.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import { NextApiRequest, NextApiResponse } from 'next'
  2. import { fetchGet } from '@/data/fetchers'
  3. import { constructHeaders } from '@/lib/api/apiHelpers'
  4. import apiWrapper from '@/lib/api/apiWrapper'
  5. import { PG_META_URL } from '@/lib/constants'
  6. export default (req: NextApiRequest, res: NextApiResponse) =>
  7. apiWrapper(req, res, handler, { withAuth: true })
  8. async function handler(req: NextApiRequest, res: NextApiResponse) {
  9. const { method } = req
  10. switch (method) {
  11. case 'GET':
  12. return handleGetAll(req, res)
  13. default:
  14. res.setHeader('Allow', ['GET'])
  15. res.status(405).json({ error: { message: `Method ${method} Not Allowed` } })
  16. }
  17. }
  18. const handleGetAll = async (req: NextApiRequest, res: NextApiResponse) => {
  19. const headers = constructHeaders(req.headers)
  20. const response = await fetchGet(`${PG_META_URL}/extensions`, { headers })
  21. if (response.error) {
  22. const { code, message } = response.error
  23. return res.status(code).json({ message })
  24. } else {
  25. return res.status(200).json(response)
  26. }
  27. }